﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.Windows.Media.Imaging;
using VAdvantage.Utility;

namespace VAdvantage.Framework
{
    public partial class Message : ChildWindow
    {
        /** Answer OK (0)       */
        public const int A_OK = 0;
        /** Answer Cancel (1)   */
        public const int A_CANCEL = 1;
        /** Answer Close (-1) - Default	*/
        public const int A_CLOSE = -1;
        /** Answer				*/
        private int _returnCode = A_CLOSE;

        // message type enum       
        public enum MessageType
        {
            ERROR,
            INFORMATION,
            QUESTION,
            WARNING,
            PLAIN
        };


        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="title">title of window</param>
        /// <param name="message">message to be displayed</param>
        /// <param name="messageType">message type</param>
        public Message(string title, string message, MessageType messageType)
        //: base(false, false, false, false, title)
        {
            base.Title = title;
            InitializeComponent();
           LayoutBorder.Background = new SolidColorBrush(DataBase.GlobalVariable.MESSAGE_BACK_COLOR);
            vlblMessage.Content = Msg.GetMsg(Envs.GetCtx(), "Message");
            // set font of this window
            //InitialFormSetting.SetFont(this);
            //mnustpMainMenuStrip.Renderer = new StartMenu.ToolStripViennaRenderer(true);

            // set the values
            //common (message, messageType);
            //this.AcceptButton = vbtnOK;
            SetInfoMessage(message);
            SetInfoIcon(messageType);
            EventHandler();
        }

        public void EventHandler()
        {
            this.KeyDown += new KeyEventHandler(Message_KeyDown);
        }

        void Message_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Ok();
            }
            if (e.Key == Key.Escape)
            {
                Cancel();
            }
        }

        /// <summary>
        /// Show the dialog
        /// </summary>
        /// <param name="message">message to be displayed</param>
        /// <returns>DialogResult</returns>
        //public static DialogResult Show(string message)
        public static void ShowMessage(string message)
        {

            if (Deployment.Current.Dispatcher.CheckAccess())
            {
                // set the values
                Message form = new Message("", message, MessageType.PLAIN);
                form.Show();    
            }
            else
            {
                Deployment.Current.Dispatcher.BeginInvoke
           (delegate
           {
               // set the values
               Message form = new Message("", message, MessageType.PLAIN);
               form.Show();
           });
            }
        }

        /// <summary>
        /// Show the dialog
        /// </summary>
        /// <param name="title">title of window</param>
        /// <param name="message">message to be displayed</param>
        /// <param name="messageType">message type</param>
        /// <returns>DialogResult</returns>
        // public static DialogResult Show(string title, string message, MessageType messageType)
        public static void ShowMessage(string title, string message, MessageType messageType)
        {
            if (Deployment.Current.Dispatcher.CheckAccess())
            {
                // set the values
                Message form = new Message(title, message, messageType);
                // opens the dialog and return whether user has clicked on OK or cancel button
                //return form.RunDialog();
                form.Show();
            }
            else
            {
                Deployment.Current.Dispatcher.BeginInvoke
           (delegate
           {
               // set the values
               Message form = new Message(title, message, messageType);
               // opens the dialog and return whether user has clicked on OK or cancel button
               //return form.RunDialog();
               form.Show();
           });
            }

        }

        /// <summary>
        /// OPens the dialog window
        /// </summary>
        /// <returns>DialogResult</returns>
        //private DialogResult RunDialog()
        //{
        //    // display window in center
        //    this.StartPosition = FormStartPosition.CenterScreen;
        //    // plays a beep sound when opened
        //    System.Media.SystemSounds.Beep.Play();
        //    // displays the dialog
        //    this.ShowDialog();
        //    // return the dialog result
        //    return this.DialogResult;
        //}

        /// <summary>
        /// Set message on the window
        /// </summary>
        /// <param name="message">message to be displayed</param>
        private void SetInfoMessage(string message)
        {
            StringBuilder sb = new StringBuilder(message.Length + 20);
            sb.Append(message);
            vlblMessageText.Text = sb.ToString().Trim();
            //int height = 0;
            //height = (Math.Max(0, message.Length / 60) + 1) * 30;
            //if (height > 600)
            //{
            //    // vlblMessageText.AutoSize = true;
            //}
            //else
            //{
            //    height = Math.Min(height, 600);
            //    vlblMessageText.Height = height;
            //}
        }

        /// <summary>
        /// Set icon according to message type
        /// </summary>
        /// <param name="messageType">message type</param>
        private void SetInfoIcon(MessageType messageType)
        {
            btnCancel.Visibility = System.Windows.Visibility.Collapsed;
            pbIcon.Visibility = System.Windows.Visibility.Visible;
            switch (messageType)
            {
                case MessageType.ERROR:
                    // error type 
                    //pbIcon.Image = VAdvantage.Properties.Resources.Error32;
                    //pbIcon.Source = new BitmapImage(new Uri("/VAdvantage;component/Images/Error32.PNG", UriKind.RelativeOrAbsolute));
                    pbIcon.Source = new BitmapImage(new Uri("/VAdvantage;component/Images/ErrorIcon32.png", UriKind.RelativeOrAbsolute));

                    //System.Windows.Media.Imaging.BitmapImage imgSource = new System.Windows.Media.Imaging.BitmapImage();
                    //imgSource.UriSource = new Uri("/VAdvantage;component/Images/read.png", UriKind.RelativeOrAbsolute);
                    //pbIcon.Source = imgSource;
                    break;
                case MessageType.INFORMATION:
                    // information type//VAdvantage.Properties.Resources.Inform32;
                  //  pbIcon.Source = new BitmapImage(new Uri("/VAdvantage;component/Images/Inform32.PNG", UriKind.RelativeOrAbsolute));
                    pbIcon.Source = new BitmapImage(new Uri("/VAdvantage;component/Images/Information32.png", UriKind.RelativeOrAbsolute));
                    break;
                case MessageType.QUESTION:
                    // question type
                    btnCancel.Visibility = System.Windows.Visibility.Visible;//  true;
                    //VAdvantage.Properties.Resources.Question32;
                   // pbIcon.Source = new BitmapImage(new Uri("/VAdvantage;component/Images/Question32.PNG", UriKind.RelativeOrAbsolute));
                    pbIcon.Source = new BitmapImage(new Uri("/VAdvantage;component/Images/Ask32.PNG", UriKind.RelativeOrAbsolute));
                    break;
                case MessageType.WARNING:
                    // warning type// VAdvantage.Properties.Resources.Warn32;
                    //pbIcon.Source = new BitmapImage(new Uri("/VAdvantage;component/Images/Warn32.PNG", UriKind.RelativeOrAbsolute));
                    pbIcon.Source = new BitmapImage(new Uri("/VAdvantage;component/Images/Warning32.PNG", UriKind.RelativeOrAbsolute));
                    break;
                case MessageType.PLAIN:
                    pbIcon.Visibility = System.Windows.Visibility.Collapsed;// false;
                    break;
                default:
                    pbIcon.Visibility = System.Windows.Visibility.Collapsed;// false;
                    break;
            }
        }

        private void mnustpiPreference_Click(object sender, EventArgs e)
        {
            UserPreference obj = new UserPreference();
            obj.Show();
        }

        //private void mnustpiEmail_Click(object sender, EventArgs e)
        //{
        //    FixedWinTemp objEmail = new FixedWinTemp();
        //    objEmail.Show();
        //}
        /**
	 *	Get Return Code
	 *  @return return code
	 */
        public int GetReturnCode()
        {
            return _returnCode;
        }

        
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Cancel();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            Ok();
        }

        public void Ok()
        {
            _returnCode = A_OK;
            this.DialogResult = true;
        }

        public void Cancel()
        {
            _returnCode = A_CANCEL;
            this.DialogResult = false;
        }

        private FrameworkElement root;
        private FrameworkElement chrome;
        private FrameworkElement contentRoot;

        private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //VisualTreeHelper.GetChildrenCount(this);
            //{
            //   // Dispatcher.BeginInvoke(() => ThisChildWindow_Loaded(this, e));
            //    return;
            //}
            //root = (FrameworkElement)VisualTreeHelper.GetChild(this, 0);
            //chrome = root.FindName("Chrome") as FrameworkElement;
            //chrome.Visibility = Visibility.Collapsed;
            
          
        }









    }
}

